#-*-coding=utf-8-*-
from google.appengine.ext import db
from google.appengine.api import users
from datetime import datetime
from random import *
from django.http import *

class File(db.Model):
    filename = db.StringProperty()
    content_type = db.StringProperty()
    content = db.BlobProperty()
    created = db.DateTimeProperty(auto_now_add = True)
    
    @classmethod
    def create(cls, file):
        filename = datetime.now().strftime("%Y%m%d%H%M%S") + str(randint(10, 99)) + file["filename"]
        file = cls(filename = filename, content_type = file["content-type"], content = db.Blob(file["content"]))
        file.put()
        return file

    @classmethod
    def get_file_by_name(cls, filename):
        file = cls.gql("where filename=:1", filename).get()
        return file
    
    @classmethod
    def get_response_by_name(cls, filename):
        file = cls.gql("where filename=:1", filename).get()
        response = HttpResponse()
        response['Content-Type'] = file.content_type
        response.write(file.content)
        return response
    
    